/*
 * @lc app=leetcode.cn id=64 lang=javascript
 *
 * [64] 最小路径和
 */

// @lc code=start
/**
 * @param {number[][]} grid
 * @return {number}
 */
var minPathSum = function(grid) {
let steps = [];
let m = grid.length;
let n = grid[0].length;
for (let index = 0; index < m; index++) {
    steps[index] = [] 
}
for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
       if(i==0&&j==0){
           steps[i][j] = grid[i][j]
       }else if(i == 0 && j!= 0){
        steps[i][j] = grid[i][j] + steps[i][j-1]
       }else if(j == 0 && i!= 0){
        steps[i][j] = grid[i][j] + steps[i-1][j]
       }
       else{
           steps[i][j] = Math.min(steps[i-1][j],steps[i][j-1])+ grid[i][j]
       }
        
    }
    
}
return steps[m-1][n-1]
};
// @lc code=end
